package com.tzy.rpc.transport.socket.server;/**
 * @description:
 * @Author HP
 * @create 2022/3/14 17:21
 */

import com.tzy.rpc.entity.RpcRequest;
import com.tzy.rpc.handler.RequestHandler;
import com.tzy.rpc.serializer.CommonSerializer;
import com.tzy.rpc.transport.socket.util.ObjectReader;
import com.tzy.rpc.transport.socket.util.ObjectWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author HP
 * @create 2022-03-14 17:21
 * @desc
 **/
public class SocketRequestHandlerThread implements Runnable{
    private static final Logger logger = LoggerFactory.getLogger(SocketRequestHandlerThread.class);

    private Socket socket;
    private RequestHandler requestHandler;
    private CommonSerializer serializer;

    public SocketRequestHandlerThread(Socket socket, RequestHandler requestHandler, CommonSerializer serializer) {
        this.socket = socket;
        this.requestHandler = requestHandler;
        this.serializer=serializer;
    }

    /**
     * 处理线程，从输入流中获取rpcRequest对象，并获取对应服务，将服务执行后得到的RpcResponse对象写入输出流
     */
    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();
            RpcRequest rpcRequest = (RpcRequest)ObjectReader.readObject(inputStream);

            Object response = requestHandler.handle(rpcRequest);
            ObjectWriter.writeObject(outputStream,response, serializer);
        } catch (IOException e) {
            logger.error("调用或发送时有错误发生：", e);
        }
    }
}
